﻿
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OZDoo.WebCommon.WebControls
{
    /// <summary>
    /// Extends IText interface by adding a click event
    /// </summary>
    public interface IButton : IText
    {
        event EventHandler Click;
        event CommandEventHandler Command;
        AttributeCollection Attributes { get; }
        string CommandArgument { get; set; }
        string CommandName { get; set; }
        bool CausesValidation { get; set; }
    }

    /// <summary>
    /// CLI implementations if IButton
    /// </summary>
    public class CLIButton : Button, IButton
    {
        public Control Control
        {
            get { return this; }
        }
    }

    /// <summary>
    /// CLI implementations if IButton
    /// </summary>
    public class CLILinkButton : LinkButton, IButton
    {
        public Control Control
        {
            get { return this; }
        }
    }

    /// <summary>
    /// Manager class for creating instance of IButton
    /// </summary>
    public class ButtonManager
    {
        public static IButton CreateButton(Button button)
        {
            if (button == null)
                throw new ArgumentNullException("button", "The button parameter can not be null");

            return new ButtonWrapper(button);
        }

        public static IButton CreateLinkButton(LinkButton button)
        {

            if (button == null)
                throw new ArgumentNullException("button", "The button parameter can not be null");

            return new LinkButtonWrapper(button);
        }

        /// <summary>
        /// Attempts to create an IButton from a Generic control reference. 
        /// </summary>
        /// <param name="cntrl">Needs to implemet IButton or derrive from Button or LinkButton</param>
        /// <returns>An instance of IButton (could be ButtonWrapper or LinkButtonWrapper)</returns>
        public static IButton Create(Control cntrl)
        {
            if (cntrl == null)
                return null;

            IButton ib = cntrl as IButton;

            if (ib == null)
            {
                if (cntrl is Button)
                    ib = CreateButton(cntrl as Button);
                else if (cntrl is LinkButton)
                    ib = CreateLinkButton(cntrl as LinkButton);
            }

            return ib;
        }

    }

    #region Button Wrapper
    /// <summary>
    /// Wrapper class for the Type Button. This allows CLI to work with "regular" Button controls (or any control
    /// which derrives from Button)
    /// </summary>
    public class ButtonWrapper : IButton
    {

        internal ButtonWrapper(Button button)
        {
            _button = button;
        }

        #region IButton Members

        public event System.EventHandler Click
        {
            add { _button.Click += value; }
            remove { _button.Click -= value; }
        }

        public event CommandEventHandler Command
        {
            add { _button.Command += value; }
            remove { _button.Command -= value; }
        }

        public bool CausesValidation
        {
            get { return _button.CausesValidation; }
            set { _button.CausesValidation = value; }
        }

        #endregion

        #region IText Members
        private Button _button = null;
        public Control Control
        {
            get { return _button; }
        }

        public bool Visible
        {
            get { return _button.Visible; }
            set { _button.Visible = value; }
        }

        public string Text
        {
            get { return _button.Text; }
            set { _button.Text = value; }
        }


        public AttributeCollection Attributes
        {
            get { return _button.Attributes; }
        }

        public string CommandArgument
        {
            get { return _button.CommandArgument; }
            set { _button.CommandArgument = value; }
        }

        public string CommandName
        {
            get { return _button.CommandName; }
            set { _button.CommandName = value; }
        }

        #endregion
    }


    #endregion

    #region LinkButton Wrapper
    /// <summary>
    /// Wrapper class for the Type LinkButton. This allows CLI to work with "regular" LinkButton controls.
    /// </summary>
    public class LinkButtonWrapper : IButton
    {

        internal LinkButtonWrapper(LinkButton button)
        {
            _button = button;
        }

        #region IButton Members

        public event System.EventHandler Click
        {
            add { _button.Click += value; }
            remove { _button.Click -= value; }
        }

        public event CommandEventHandler Command
        {
            add { _button.Command += value; }
            remove { _button.Command -= value; }
        }

        public string CommandArgument
        {
            get { return _button.CommandArgument; }
            set { _button.CommandArgument = value; }
        }

        public string CommandName
        {
            get { return _button.CommandName; }
            set { _button.CommandName = value; }
        }

        public bool CausesValidation
        {
            get { return _button.CausesValidation; }
            set { _button.CausesValidation = value; }
        }

        #endregion

        #region IText Members
        private LinkButton _button = null;
        public Control Control
        {
            get { return _button; }
        }

        public bool Visible
        {
            get { return _button.Visible; }
            set { _button.Visible = value; }
        }

        public string Text
        {
            get { return _button.Text; }
            set { _button.Text = value; }
        }


        public AttributeCollection Attributes
        {
            get { return _button.Attributes; }
        }

        #endregion

    }

    #endregion
}
